package com.algrithom.stack;

import java.util.Stack;
import java.util.stream.Collectors;

/**
 * 最小栈
 *
 * @author think
 */
public class MinStack {
    
    Stack<Integer> stack = new Stack<>();
    
    /**
     * initialize your data structure here.
     */
    public MinStack(){}
    
    public static void main(String[] args){
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin()); // 返回 -3.
        minStack.pop();
        System.out.println(minStack.top()); // 返回 0.
        System.out.println(minStack.getMin()); // 返回 -2.
    }
    
    public void push(int x){
        stack.push(x);
    }
    
    public void pop(){
        stack.pop();
    }
    
    public int top(){
        return stack.peek();
    }
    
    public int getMin(){
        Stack<Integer> collect = stack.stream().sorted().collect(Collectors.toCollection(Stack::new));
        return stack.get(stack.size() - stack.search(collect.get(0)));
    }
}
